﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:RoleInfo
	/// </summary>
	public partial class RoleInfo
	{
		public RoleInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from RoleInfo");
			strSql.Append(" where name=@name ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String)			};
			parameters[0].Value = name;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.RoleInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into RoleInfo(");
			strSql.Append("name,password,qq,sex,setupDate,hp,mp,st,de,totalpoint,sypoint,baseskill,skills,totalVictory,totalFailed,NowVictory,NowFailed,IsFirst,isPaidUser,nickName)");
			strSql.Append(" values (");
			strSql.Append("@name,@password,@qq,@sex,@setupDate,@hp,@mp,@st,@de,@totalpoint,@sypoint,@baseskill,@skills,@totalVictory,@totalFailed,@NowVictory,@NowFailed,@IsFirst,@isPaidUser,@nickName)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String),
					new SQLiteParameter("@password", DbType.String),
					new SQLiteParameter("@qq", DbType.String),
					new SQLiteParameter("@sex", DbType.String),
					new SQLiteParameter("@setupDate", DbType.String),
					new SQLiteParameter("@hp", DbType.Int32,4),
					new SQLiteParameter("@mp", DbType.Int32,4),
					new SQLiteParameter("@st", DbType.Int32,4),
					new SQLiteParameter("@de", DbType.Int32,4),
					new SQLiteParameter("@totalpoint", DbType.Int32,4),
					new SQLiteParameter("@sypoint", DbType.Int32,4),
					new SQLiteParameter("@baseskill", DbType.String),
					new SQLiteParameter("@skills", DbType.String),
					new SQLiteParameter("@totalVictory", DbType.Int32,4),
					new SQLiteParameter("@totalFailed", DbType.Int32,4),
					new SQLiteParameter("@NowVictory", DbType.Int32,4),
					new SQLiteParameter("@NowFailed", DbType.Int32,4),
					new SQLiteParameter("@IsFirst", DbType.Int32,4),
					new SQLiteParameter("@isPaidUser", DbType.Int32,4),
					new SQLiteParameter("@nickName", DbType.String)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.password;
			parameters[2].Value = model.qq;
			parameters[3].Value = model.sex;
			parameters[4].Value = model.setupDate;
			parameters[5].Value = model.hp;
			parameters[6].Value = model.mp;
			parameters[7].Value = model.st;
			parameters[8].Value = model.de;
			parameters[9].Value = model.totalpoint;
			parameters[10].Value = model.sypoint;
			parameters[11].Value = model.baseskill;
			parameters[12].Value = model.skills;
			parameters[13].Value = model.totalVictory;
			parameters[14].Value = model.totalFailed;
			parameters[15].Value = model.NowVictory;
			parameters[16].Value = model.NowFailed;
			parameters[17].Value = model.IsFirst;
			parameters[18].Value = model.isPaidUser;
			parameters[19].Value = model.nickName;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.RoleInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update RoleInfo set ");
			strSql.Append("password=@password,");
			strSql.Append("qq=@qq,");
			strSql.Append("sex=@sex,");
			strSql.Append("setupDate=@setupDate,");
			strSql.Append("hp=@hp,");
			strSql.Append("mp=@mp,");
			strSql.Append("st=@st,");
			strSql.Append("de=@de,");
			strSql.Append("totalpoint=@totalpoint,");
			strSql.Append("sypoint=@sypoint,");
			strSql.Append("baseskill=@baseskill,");
			strSql.Append("skills=@skills,");
			strSql.Append("totalVictory=@totalVictory,");
			strSql.Append("totalFailed=@totalFailed,");
			strSql.Append("NowVictory=@NowVictory,");
			strSql.Append("NowFailed=@NowFailed,");
			strSql.Append("IsFirst=@IsFirst,");
			strSql.Append("isPaidUser=@isPaidUser,");
			strSql.Append("nickName=@nickName");
			strSql.Append(" where name=@name ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@password", DbType.String),
					new SQLiteParameter("@qq", DbType.String),
					new SQLiteParameter("@sex", DbType.String),
					new SQLiteParameter("@setupDate", DbType.String),
					new SQLiteParameter("@hp", DbType.Int32,4),
					new SQLiteParameter("@mp", DbType.Int32,4),
					new SQLiteParameter("@st", DbType.Int32,4),
					new SQLiteParameter("@de", DbType.Int32,4),
					new SQLiteParameter("@totalpoint", DbType.Int32,4),
					new SQLiteParameter("@sypoint", DbType.Int32,4),
					new SQLiteParameter("@baseskill", DbType.String),
					new SQLiteParameter("@skills", DbType.String),
					new SQLiteParameter("@totalVictory", DbType.Int32,4),
					new SQLiteParameter("@totalFailed", DbType.Int32,4),
					new SQLiteParameter("@NowVictory", DbType.Int32,4),
					new SQLiteParameter("@NowFailed", DbType.Int32,4),
					new SQLiteParameter("@IsFirst", DbType.Int32,4),
					new SQLiteParameter("@isPaidUser", DbType.Int32,4),
					new SQLiteParameter("@nickName", DbType.String),
					new SQLiteParameter("@name", DbType.String)};
			parameters[0].Value = model.password;
			parameters[1].Value = model.qq;
			parameters[2].Value = model.sex;
			parameters[3].Value = model.setupDate;
			parameters[4].Value = model.hp;
			parameters[5].Value = model.mp;
			parameters[6].Value = model.st;
			parameters[7].Value = model.de;
			parameters[8].Value = model.totalpoint;
			parameters[9].Value = model.sypoint;
			parameters[10].Value = model.baseskill;
			parameters[11].Value = model.skills;
			parameters[12].Value = model.totalVictory;
			parameters[13].Value = model.totalFailed;
			parameters[14].Value = model.NowVictory;
			parameters[15].Value = model.NowFailed;
			parameters[16].Value = model.IsFirst;
			parameters[17].Value = model.isPaidUser;
			parameters[18].Value = model.nickName;
			parameters[19].Value = model.name;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string name)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from RoleInfo ");
			strSql.Append(" where name=@name ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String)			};
			parameters[0].Value = name;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string namelist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from RoleInfo ");
			strSql.Append(" where name in ("+namelist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.RoleInfo GetModel(string name)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select name,password,qq,sex,setupDate,hp,mp,st,de,totalpoint,sypoint,baseskill,skills,totalVictory,totalFailed,NowVictory,NowFailed,IsFirst,isPaidUser,nickName from RoleInfo ");
			strSql.Append(" where name=@name ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String)			};
			parameters[0].Value = name;

			Maticsoft.Model.RoleInfo model=new Maticsoft.Model.RoleInfo();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.RoleInfo DataRowToModel(DataRow row)
		{
			Maticsoft.Model.RoleInfo model=new Maticsoft.Model.RoleInfo();
			if (row != null)
			{
				if(row["name"]!=null)
				{
					model.name=row["name"].ToString();
				}
				if(row["password"]!=null)
				{
					model.password=row["password"].ToString();
				}
				if(row["qq"]!=null)
				{
					model.qq=row["qq"].ToString();
				}
				if(row["sex"]!=null)
				{
					model.sex=row["sex"].ToString();
				}
				if(row["setupDate"]!=null)
				{
					model.setupDate=row["setupDate"].ToString();
				}
				if(row["hp"]!=null && row["hp"].ToString()!="")
				{
					model.hp=int.Parse(row["hp"].ToString());
				}
				if(row["mp"]!=null && row["mp"].ToString()!="")
				{
					model.mp=int.Parse(row["mp"].ToString());
				}
				if(row["st"]!=null && row["st"].ToString()!="")
				{
					model.st=int.Parse(row["st"].ToString());
				}
				if(row["de"]!=null && row["de"].ToString()!="")
				{
					model.de=int.Parse(row["de"].ToString());
				}
				if(row["totalpoint"]!=null && row["totalpoint"].ToString()!="")
				{
					model.totalpoint=int.Parse(row["totalpoint"].ToString());
				}
				if(row["sypoint"]!=null && row["sypoint"].ToString()!="")
				{
					model.sypoint=int.Parse(row["sypoint"].ToString());
				}
				if(row["baseskill"]!=null)
				{
					model.baseskill=row["baseskill"].ToString();
				}
				if(row["skills"]!=null)
				{
					model.skills=row["skills"].ToString();
				}
				if(row["totalVictory"]!=null && row["totalVictory"].ToString()!="")
				{
					model.totalVictory=int.Parse(row["totalVictory"].ToString());
				}
				if(row["totalFailed"]!=null && row["totalFailed"].ToString()!="")
				{
					model.totalFailed=int.Parse(row["totalFailed"].ToString());
				}
				if(row["NowVictory"]!=null && row["NowVictory"].ToString()!="")
				{
					model.NowVictory=int.Parse(row["NowVictory"].ToString());
				}
				if(row["NowFailed"]!=null && row["NowFailed"].ToString()!="")
				{
					model.NowFailed=int.Parse(row["NowFailed"].ToString());
				}
				if(row["IsFirst"]!=null && row["IsFirst"].ToString()!="")
				{
					model.IsFirst=int.Parse(row["IsFirst"].ToString());
				}
				if(row["isPaidUser"]!=null && row["isPaidUser"].ToString()!="")
				{
					model.isPaidUser=int.Parse(row["isPaidUser"].ToString());
				}
				if(row["nickName"]!=null)
				{
					model.nickName=row["nickName"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select name,password,qq,sex,setupDate,hp,mp,st,de,totalpoint,sypoint,baseskill,skills,totalVictory,totalFailed,NowVictory,NowFailed,IsFirst,isPaidUser,nickName ");
			strSql.Append(" FROM RoleInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM RoleInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.name desc");
			}
			strSql.Append(")AS Row, T.*  from RoleInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "RoleInfo";
			parameters[1].Value = "name";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

